package edu.pku.sei.transformation.constraint;

import java.util.HashSet;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;

import edu.pku.sei.transformation.editor.model.test.CheckPoint;

public class ConstraintEngineRegister {
	private HashSet<Class> register = new HashSet<Class>();
	public static final ConstraintEngineRegister INSTANCE = new ConstraintEngineRegister();
	private ConstraintEngineRegister() {
		
	}
	public boolean regist(Class engine) {
		if (ConstraintEngine.class.isAssignableFrom(engine)) {
			register.add(engine);
			return true;
		} else {
			return false;
		}
	}
	
	public IMenuManager getMenu(Object obj) {
		IMenuManager actions = new MenuManager("check constraint");
		if (obj instanceof CheckPoint) {
			CheckPoint cp = (CheckPoint) obj;
			for (Class cls : register) {
				try {
					ConstraintEngine engine = (ConstraintEngine) cls.newInstance();
					CheckConstraintAction action = new CheckConstraintAction(cp, engine);
					actions.add(action);
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		return actions;
	}
}
